import requests
import time
import re
import json


def a():
    # requests.
    url = 'http://cn-sdjn2-cu-v-09.acgvideo.com/upgcxcode/45/21/112492145/112492145_nb2-1-30064.m4s?expires=1570296900&platform=pc&ssig=VGmPG261bHIglk7CeSWlXA&oi=660165983&trid=dfafbb2258a1406f816af0aaf26b9b04u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mid=74236400'
    url2 = 'http://cn-hbcd2-cu-v-05.acgvideo.com/upgcxcode/45/21/112492145/112492145_nb2-1-30080.m4s?expires=1570296900&platform=pc&ssig=0iOcvquYagOFLg0_T7w3kQ&oi=660165983&trid=dfafbb2258a1406f816af0aaf26b9b04u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mid=74236400'
    url3 = 'http://cn-hbcd2-cu-v-05.acgvideo.com/upgcxcode/45/21/112492145/112492145_nb2-1-30080.m4s'
    url4 = 'http://cn-hbcd2-cu-v-05.acgvideo.com/upgcxcode/45/21/112492145/112492145_nb2-1-30080.m4s?expires=1570299600&platform=pc&ssig=TxZM5losEAu1W0kGX33yrg&oi=660165983&trid=7d674b72e4ae4c61a1b51c7ef12e50c1u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mid=74236400'
    referer = 'https://www.bilibili.com/video/av63096724'
    url_a = 'http://cn-hncs-cu-v-01.acgvideo.com/upgcxcode/21/46/75424621/75424621-1-30216.m4s?expires=1570351500&platform=pc&ssig=avbAVHm4pXOjYqWDCbICZw&oi=660165983&trid=9dbc8b3059df44b4a2ec4bcf86188f35u&nfc=1&nfb=maPYqpoel5MI3qOUX6YpRA==&mid=74236400'
    url_b = 'http://upos-hz-mirrorkodou.acgvideo.com/upgcxcode/39/76/110697639/110697639-1-30216.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1570351549&gen=playurl&os=kodou&oi=660165983&trid=ccfb0d283d2a4028919b5ec32bf401eau&platform=pc&upsig=bbfb0b05dd0f9e172f20d7ec78f60e1a&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=74236400'
    url_c = 'http://upos-hz-mirrorwcsu.acgvideo.com/upgcxcode/39/76/110697639/110697639_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1570351549&gen=playurl&os=wcsu&oi=660165983&trid=ccfb0d283d2a4028919b5ec32bf401eau&platform=pc&upsig=5f8d530c98d4cda13ddf1699919c89ea&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=74236400'

    download(url_c, '/home/xieweig/document/bilibili/11ab.mp4')


def download(url, path):
    start = time.time()  # 开始时间

    size = 0

    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'Referer': 'https://www.bilibili.com/video/av63096724'
    }

    response = requests.get(url, headers=headers, stream=True)  # stream属性必须带上

    chunk_size = 1024  # 每次下载的数据大小
    content_size = int(response.headers['content-length'])  # 总大小

    if response.status_code == 200:
        print('[文件大小]:%0.2f MB' % (content_size / chunk_size / 1024))  # 换算单位
        with open(path, 'wb') as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)

                size += len(data)  # 已下载的文件大小


def login():
    username = '18300284060ss'
    password = '123456'
    url = 'http://127.0.0.1:8080/login?username={}&password={}' \
        .format(username, password)
    res = requests.post(url)
    print(res.status_code, res.headers)
    if 'Set-Cookie' in res.headers.keys():
        print(res.headers['Set-Cookie'])
    print(res.text)


def logout():
    headers = {
        'Host': '127.0.0.1',
        'Accept-Language': 'en-US,en;q=0.5',
        'Content-Type': 'application/json;charset=UTF-8'
    }

    cookies = {'JRedisId': 'coolsessions:dba0f830-174b-4df2-a222-cd032724c556'}

    url = 'http://127.0.0.1:8080/logout'
    res = requests.post(url, cookies=cookies, headers=headers)
    print( res.status_code,res.cookies,res.headers)

def getScoreById():
    cookies = {'JRedisId': 'coolsessions:dba0f830-174b-4df2-a222-cd032724c556'}
    headers = {
        'Host': '127.0.0.1',
        'Accept-Language': 'en-US,en;q=0.5',
        'Content-Type': 'application/json;charset=UTF-8'
    }
    url='http://127.0.0.1:8080/score/s_581100'
    res =requests.get(url,cookies=cookies,headers=headers)
    print(res.status_code,res.text)
if __name__ == '__main__':
      login()
     # getScoreById()
    # logout()
